package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.base.Code;
import me.zhengjie.enums.DeletedEnum;
import me.zhengjie.modules.system.converter.MachineConverter;
import me.zhengjie.modules.system.domain.entity.SysUserIdentityCard;
import me.zhengjie.modules.system.domain.req.SysUserIdentityCardReqDTO;
import me.zhengjie.modules.system.domain.req.SysUserIdentityCardReqQueryDTO;
import me.zhengjie.modules.system.domain.rsp.SysUserIdentityCardRspDTO;
import me.zhengjie.modules.system.mapper.SysUserIdentityCardMapper;
import me.zhengjie.modules.system.service.SysUserIdentityCardService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@RequiredArgsConstructor
public class SysUserIdentityCardServiceImpl extends ServiceImpl<SysUserIdentityCardMapper, SysUserIdentityCard> implements
        SysUserIdentityCardService {


    /**
     * 添加
     *
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(SysUserIdentityCardReqDTO reqDTO) {
        SysUserIdentityCard sysUserIdentityCard = MachineConverter.INSTANCE.sysUserIdentityCardReqDTOTo(reqDTO);
        return super.save(sysUserIdentityCard);
    }

    /**
     * 修改
     *
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SysUserIdentityCardReqDTO reqDTO) {
        SysUserIdentityCard sysUserIdentityCard = MachineConverter.INSTANCE.sysUserIdentityCardReqDTOTo(reqDTO);
        return super.updateById(sysUserIdentityCard);
    }


    /**
     * 删除
     *
     * @param code
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Code code) {
        return super.removeById(code.getCode());
    }

    /**
     * 主键查询
     *
     * @param code
     */
    public SysUserIdentityCardRspDTO findById(Code code) {
        SysUserIdentityCard sysUserIdentityCard = lambdaQuery().eq(SysUserIdentityCard::getId, code.getCode())
                .eq(SysUserIdentityCard::getIsDel, DeletedEnum.YES.getCode())
                .one();
        return MachineConverter.INSTANCE.sysUserIdentityCardToRsp(sysUserIdentityCard);
    }

    /**
     * 列表
     *
     * @param reqQueryDTO
     * @return
     */
    public PageResult<SysUserIdentityCardRspDTO> findAll(SysUserIdentityCardReqQueryDTO reqQueryDTO) {
        PageHelper.startPage(reqQueryDTO.getPageNo(), reqQueryDTO.getPageSize(), true);
        List<SysUserIdentityCardRspDTO> list = getBaseMapper().findAll(reqQueryDTO);
        PageInfo<SysUserIdentityCardRspDTO> pageInfo = new PageInfo<>(list);
        return PageUtil.toPage(pageInfo.getList(), pageInfo.getTotal());
    }

}
